<?php
/**
 * ClassName: TreatmentServicesService
 * Description: 给病人开的治疗服务 service
 * @Author 周昌
 * @Create 2023/11/17 16:19
 */

namespace service\admin;


use Exception;
use libs\Tool;
use model\AdminModel;
use model\CasesModel;
use model\TreatmentCateModel;
use model\TreatmentModel;
use model\TreatmentServicesModel;
use service\BaseService;
use think\Db;

class TreatmentServicesService extends BaseService
{
    protected static $model='model\TreatmentServicesModel';
    public static $services_status=[0=>'未操作',1=>'已操作',2=>'已取消'];//服务状态
    public static $services_status_index=['no'=>0,'yes'=>1,'cancel'=>2];

    //添加/修改
    public static function addEdit($case_id,$data)
    {
        if(empty($data)||empty(array_filter($data['treatment_id']))){
            return Tool::Error('请添加服务项');
        }
        $insert=[];
        $caseInfo=CasesModel::getInfo(['id'=>$case_id]);
        $baseInfo=[
            'archives_id'=>$caseInfo['archives_id'],
            'case_id'=>$caseInfo['id'],
            'hospital_id'=>$caseInfo['hospital_id'],
//            'admin_id'=>$caseInfo['admin_id'],
//            'admin_name'=>AdminModel::getValue(['id'=>$caseInfo['admin_id']],'group_name'),
            'addtime'=>time(),
            'update_time'=>time(),
        ];
        //treatment_name
        $treatment_names=TreatmentModel::getListColumn([['id','in',$data['treatment_id']]],'id,name');
        //批量处理data数据 treatment_id price
        foreach ($data['treatment_id'] as $k=>$v){
            $row=[];
            $row['treatment_id']=$v;
            $row['treatment_name']=$treatment_names[$v];
            $row['money']=$data['price'][$k];
            $row['money']=$data['price'][$k];
            $insert[]=array_merge($baseInfo,$row);
        }
        Db::startTrans();
        try {
            $del_res=self::$model::delInfo(['case_id'=>$case_id]);
            $insert_res=self::$model::insertMore($insert);
            if($del_res===false||!$insert_res){
                Db::rollback();
                return  Tool::Error();
            }
            Db::commit();
            return Tool::Success();
        }catch (Exception $exception){
            Db::rollback();
            return  Tool::Error($exception->getMessage());
        }


    }
    //问诊 获取已开治疗项目
    public static function getTreatmentServices($case_id)
    {
        $data=TreatmentServicesModel::getList(['case_id'=>$case_id],'id,treatment_id,treatment_name,money');
        if($data){
            $treatment_ids=array_column($data,'treatment_id');
            $treatment_cate_ids=TreatmentModel::getListColumn([['id','in',$treatment_ids]],'id,treatment_cate_id');
            $treatment_cate_ids=array_filter($treatment_cate_ids);
            $treatment_list=TreatmentCateModel::where('id','in',$treatment_cate_ids)->field('id')->with(['treatment'=>function($query){
                $query->field('id,treatment_cate_id,name,price')->select();
            }])->select()->toArray();
            foreach ($data as $k=>&$v){
                $v['treatment_cate_id']=$treatment_cate_ids[$v['treatment_id']];
                $v['treatment']=[];
                foreach ($treatment_list as $n){
                    if($v['treatment_cate_id']==$n['id']){
                        $data[$k]['treatment']=$n['treatment'];
                    }
                }
            }

        }
        return $data;

    }
    //获取 病历治疗服务 列表数据
    public static function getServicesList($where)
    {
        $list=self::$model::getList($where);
        if(!empty($list)){
            $status=self::$services_status;
            $list=array_map(function ($v)use ($status){
                $v['status_name']=$status[$v['status']];
                return $v;
            },$list);
        }
        return $list;
    }
    //列表数据
    public static function getAjaxData($where)
    {
        $table = self::$model::order('id desc');
        $adminInfo=self::getAdminInfo();
        return self::tableJsonData($table, $where, function ($data)use($adminInfo){
            if($adminInfo['role']==1){
                $data['treatment_cate_name']=TreatmentCateService::getNameByKey($data['treatment_cate_id']);
            }else{
                $data['treatment_cate_name']=TreatmentCateService::getNameByKey($data['treatment_cate_id'],['hospital_id'=>$adminInfo['hospital_id']]);
            }
            $data['hospital_name'] = $data['hospital_id']?HospitalService::getNameByKey($data['hospital_id'],'','id,name','id','hospital_key_name'):'';
            return $data;
        });
    }
    


}